﻿using System;
using System.Drawing;

namespace GeoSketch2.Core
{
    /// <summary>
    /// An arc object, for the draw arc method
    /// </summary>
    public class Arc
    {
        #region Fields
        /// <summary>
        /// The rectangle which contains the ellipse, from which an arc is extracted
        /// </summary>
        public RectangleF EllipseBound;

        /// <summary>
        /// The start angle of the ellipse
        /// </summary>
        public float StartAngle;

        /// <summary>
        /// The sweep angle of the arc, + is clockwise from the start point
        /// </summary>
        public float SweepAngle; 
        #endregion

        #region Constructor
        /// <summary>
        /// An arc object from 3 points
        /// </summary>
        /// <param name="Point1">The first point</param>
        /// <param name="Point2">The second point</param>
        /// <param name="Point3">The third point</param>
        public Arc(Point2D64D Point1, Point2D64D Point2, Point2D64D Point3)
        {
            Point3D64D Circle = GeoMath.CircleFrom3Points(Point1, Point2, Point3);
            EllipseBound = new RectangleF((float)(Circle.X - Circle.Z), (float)(Circle.Y - Circle.Z), (float)(2 * Circle.Z), (float)(2 * Circle.Z));
            StartAngle = (float)((Math.Atan2(Point1.Y - Circle.Y, Point1.X - Circle.X).Radians2Degrees() + 360) % 360);
            float Anglepoint2 = (float)((Math.Atan2(Point2.Y - Circle.Y, Point2.X - Circle.X).Radians2Degrees() + 360) % 360);
            float Anglepoint3 = (float)((Math.Atan2(Point3.Y - Circle.Y, Point3.X - Circle.X).Radians2Degrees() + 360) % 360);
            SweepAngle = Anglepoint3 - StartAngle;
            if ((Anglepoint2 < StartAngle) != (Anglepoint2 > Anglepoint3))
            {
                if (SweepAngle < 0)
                {
                    SweepAngle = SweepAngle + 360;
                }
                else
                {
                    SweepAngle = SweepAngle - 360;
                }
            }
        } 
        #endregion
    }
}
